$import( "SmitearWebGL.Objects.Canvas.ScreenQuad" );
$import( "SmitearWebGL.Programs.Hdr.AdaptationProgram" );

SmitearWebGL.Objects.Canvas.Hdr.Adaptation = SmitearWebGL.Objects.Canvas.ScreenQuad.extend(
{
	/******** constructor ********/

	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Objects.Canvas.Hdr.Adaptation" );
		
		this._adaptationTech = null;
		
		this._dsOffsetsX = null;
		this._dsOffsetsY = null;
	}
	,
	calculateOffsets : function()
	{
		var i, j, idx;
		
		this._dsOffsetsX = new Float32Array( 16 );
		this._dsOffsetsY = new Float32Array( 16 );
		idx = 0;
		for( i = -2; i < 2; i++ )
		{
			for( j = -2; j < 2; j++ )
			{
				this._dsOffsetsX[idx] = ( i + 0.5 ) * ( 1.0 / 4.0 );
				this._dsOffsetsY[idx] = ( j + 0.5 ) * ( 1.0 / 4.0 );
				idx ++;
			}
		}
	}
	,
	
	//override render function
	renderCallBack : function( renderSession )
	{
		var program = this._adaptationTech;
		
		var hr = SmitearWebGL.Core.HResult.SUCCESS;

		hr |= program.activeShader();
		hr |= program.activeUniform( renderSession, this );
		
		hr |= this._vertexBuffer.bind();
		hr |= this._vertexBuffer.active( program.getShader() );
		
		this.context.gl.drawArrays( this.context.gl.TRIANGLE_STRIP, 0, 4 );
		
		this.swapTexture(1, 2);
		
		return hr;
	}
	,
	_createResource : function( params )
	{
		this._super();
		
		this._adaptationTech = this.context.programManager.getResourceByName( SmitearWebGL.Programs.InternalPrograms.Type.HDR_ADAPTATION );
	}
	,
	_destroyResource : function()
	{
		this._super();
		
		this._adaptationTech = null;
		
		this._dsOffsetsX = null;
		this._dsOffsetsY = null;
	}
}
);
